# -*- coding: UTF-8 -*-

import gtk
from codes import CodesWindow

class DisplayFrame:
    def __init__(self, display):
        self.display = display

    def make(self):
        display_aspect_frame = gtk.AspectFrame(_("How display applet?"))
        display_vbox = gtk.VBox()
        display_aspect_frame.add(display_vbox)
        button = gtk.RadioButton(None, _("Only icon"))
        button.set_size_request(200, -1)
        button.connect("toggled", self.__change_display, 1)
        if self.display == 1:
            button.set_active(True)
        display_vbox.pack_start(button)
        button.show()
        button = gtk.RadioButton(button, _("Only text"))
        button.connect("toggled", self.__change_display, 2)
        if self.display == 2:
            button.set_active(True)
        display_vbox.pack_start(button)
        button.show()
        button = gtk.RadioButton(button, _("Icon and text"))
        button.connect("toggled", self.__change_display, 0)
        if self.display == 0:
            button.set_active(True)
        display_vbox.pack_start(button)
        button.show()
        display_vbox.show()
        display_aspect_frame.show()
        return display_aspect_frame
        
    def __change_display(self, widget, data=0):
        self.display = data
        
class ServiceFrame:
    def __init__(self, services, service):
        self.services = services
        self.service = service
        
    def make(self):
        services_aspect_frame = gtk.AspectFrame(_("Use service"))
        services_vbox = gtk.VBox()
        services_aspect_frame.add(services_vbox)
    
        button = None
        for service in self.services.modules:
            if button is None:
                button = gtk.RadioButton(None, service)
            else:
                button = gtk.RadioButton(button, service)
            button.set_size_request(200, -1)
            button.connect("toggled", self.__change_service, service)
            if service == self.service.__label__:
                button.set_active(True)
            services_vbox.pack_start(button)
            button.show()
            
        services_vbox.show()
        services_aspect_frame.show()
        return services_aspect_frame
        
    def __change_service(self, widget, data="Imageshack.us"):
        self.service = self.services.modules[data]

class SettingsWindow:
    def __init__(self, gconf, codes):
        self.gconf = gconf
        self.codes = codes

    def make(self, display, services, service):
        self.setting_dialog = gtk.Dialog(_("Preferences"), None,
            gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
            (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
             gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
        
        self.setting_dialog.vbox.set_spacing(5)
        
        displayobj = DisplayFrame(display)
        display_aspect_frame = displayobj.make()
        self.setting_dialog.vbox.pack_start(display_aspect_frame)
        
        serviceobj = ServiceFrame(services, service)
        service_aspect_frame = serviceobj.make()
        self.setting_dialog.vbox.pack_start(service_aspect_frame)
        
        button = gtk.Button(label=_("Templates"))
        button.connect("clicked", self._codes_window)
        button.show()
        self.setting_dialog.vbox.pack_start(button)
        
        response = self.setting_dialog.run()
        if response == gtk.RESPONSE_ACCEPT:
            self._preferences_save(displayobj, serviceobj)
        self.__destroy_ppm_preferences(0)
        
    def _codes_window(self, widget, data=0):
        window = CodesWindow(self.codes)
        window.make(self.setting_dialog)
        
    def __destroy_ppm_preferences(self, event):
        self.setting_dialog.destroy()
        
    def _preferences_save(self, displayobj, serviceobj):
        self.gconf.set_display(displayobj.display)
        self.gconf.set_service(serviceobj.service.__label__)
